package com.tjsinfo.tjsoa.fileDocument.bo;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.fileDocument.dao.IFileDocumentFileDAO;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentFile;

public class FileDocumentFileBo implements IFileDocumentFileBo {
	private IFileDocumentFileDAO documentFileDAO;
	private IJDBCDao jdbcdao;
	private IQbcDao qbcDao;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#getJdbcdao()
	 */
	public IJDBCDao getJdbcdao() {
		return jdbcdao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#setJdbcdao(com.tjsinfo.tjsoa.Dao.IJDBCDao)
	 */
	public void setJdbcdao(IJDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#getDocumentFileDAO()
	 */
	public IFileDocumentFileDAO getDocumentFileDAO() {
		return documentFileDAO;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#setDocumentFileDAO(com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileDAO)
	 */
	public void setDocumentFileDAO(IFileDocumentFileDAO documentFileDAO) {
		this.documentFileDAO = documentFileDAO;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileBo#save(com.tjsinfo.tjsoa.fileExpress.FileExpress_File.FileExpressFile)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#save(com.tjsinfo.tjsoa.fileDocument.FileDocument_File.FileDocumentFile)
	 */
	public void save(FileDocumentFile file) {
		documentFileDAO.save(file);
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileBo#update(com.tjsinfo.tjsoa.fileExpress.FileExpress_File.FileExpressFile)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#update(com.tjsinfo.tjsoa.fileDocument.FileDocument_File.FileDocumentFile)
	 */
	public void update(FileDocumentFile file) {
		documentFileDAO.update(file);
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#updatejdbc(int, int)
	 */
	public void updatejdbc(String id,int state) {
		String sql = "update dbo.t_fileDocument_file set state="+state+" where id="+id+"";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#updatejdbcName(int, java.lang.String)
	 */
	public void updatejdbcName(String id,String name) {
		String sql = "update dbo.t_fileDocument_file set fileName='"+name+"' where id='"+id+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#deleteGuoQi(int, java.lang.String)
	 */
	public void deleteGuoQi(String id,String path) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		SimpleExpression a = Restrictions.eq("state", 1);
		criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("fileDocumentGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("id", id);
		criteria2.add(b);
		
		List<FileDocumentFile> list = qbcDao.getCriteria(criteria);
		String sql = "delete from dbo.t_fileDocument_file where id in(";
		if (list.size()>0) {
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				FileDocumentFile file = (FileDocumentFile) iterator.next();
				String name = file.getFileName();
				String strdos = "cmd /c del \""+path+name+"\"";
	 		   	Process process;
				try {
					process = Runtime.getRuntime().exec(strdos);
					process.waitFor();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sql += "'"+file.getId()+"',";
			}
			sql = sql.substring(0, sql.length()-1)+")";
			jdbcdao.executeUpdate(sql);
			jdbcdao.closeStmt();
			jdbcdao.closeConn();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#deleteFile(java.lang.String, int, java.lang.String)
	 */
	public String deleteFile(String ids,String dirName) {
		String dirName1 = "";
		String[] inte = new String[ids.split(",").length];
		for (int j = 0; j < inte.length; j++) {
			if (ids.split(",")[j] != null) {
				inte[j] = ids.split(",")[j];
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		Criterion a = Restrictions.in("id", inte);
		criteria.add(a);
		List list = qbcDao.getCriteria(criteria);
		
		String str = "";
		boolean bl = false;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileDocumentFile file = (FileDocumentFile) iterator.next();
			String name = file.getFileName();
			String path = file.getFilePath();
			
			ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.fileDocument.properties.systemResource_zh_CN");
			dirName1 = bundle.getString("fileupload")+"\\";
			
			try {
				File f = new File(file.getFilePath());
				
				if(!f.renameTo(f)){
					str += name+",";
					bl = true;
				}
				if(f.exists()){
					f.delete();
				}
			} catch (RuntimeException e1) {
			}finally{
				String sql = "delete from dbo.t_fileDocument_file where id='"+file.getId()+"'";
				jdbcdao.executeUpdate(sql);
				jdbcdao.closeStmt();
				jdbcdao.closeConn();
				
//				String strdos = "cmd /c del \""+file.getFilePath()+"\"";
//				System.out.println(strdos);
//				Process process;
//				try {
//					process = Runtime.getRuntime().exec(strdos);
//					process.waitFor();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}
		}
		if (bl) {
			str += "其他用户正在下载，请稍后进行删除！";
		}else {
			str = "ok";
		}
		return str;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#findById(int)
	 */
	public FileDocumentFile findById(String id) {
		FileDocumentFile file = documentFileDAO.findById(id);
		return file;
	}
	
	public FileDocumentFile findByFileName(String fileName) {
		List list = documentFileDAO.findByProperty("fileName", fileName);
		FileDocumentFile file = null;
		if(list.size()>0){
			file = (FileDocumentFile) list.iterator().next();
		}
		return file;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#MoveFile(java.lang.String, java.lang.String)
	 */
	public void MoveFile(String copyid,String copygroup) {
		String sql = "update dbo.t_fileDocument_file set group_id='"+copygroup+"' where id in("+copyid+")";
		jdbcdao.executeUpdate(sql);
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#getExpressFile(int)
	 */
	public List getExpressFile(int userid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		SimpleExpression a = Restrictions.eq("state", 0);
		criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("id", userid);
		criteria1.add(b);
		criteria.addOrder(Order.desc("datetime"));
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#selectFileByName(java.lang.String, java.lang.Integer)
	 */
	public List selectFileByName(String fileName,Integer user_id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		SimpleExpression a = Restrictions.eq("state", 0);
		SimpleExpression b = Restrictions.like("fileName", "%@&_&@%"+fileName+"%");
		LogicalExpression c = Restrictions.and(a, b);
		criteria.add(c);
		
		DetachedCriteria criteria1 = criteria.createCriteria("fileDocumentGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression d = Restrictions.eq("id", user_id);
		criteria2.add(d);
		
		criteria.addOrder(Order.desc("datetime"));
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#selectHighFile(java.lang.String, java.lang.String, java.lang.Integer)
	 */
	public List selectHighFile(String lie,String value,Integer userid) {
		Date date = null;
		Date date1 = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		SimpleExpression a = Restrictions.eq("state", 0);
		criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("fileDocumentGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("id", userid);
		criteria2.add(b);
		if (lie.equals("fileName")) {
			SimpleExpression c = Restrictions.like("fileName", "%@&_&@%"+value+"%");
			criteria.add(c);
		}else if (lie.equals("username")) {
			DetachedCriteria criteria3 = criteria.createCriteria("tjsUser");
			SimpleExpression d = Restrictions.eq("userName", value);
			criteria3.add(d);
		}else if (lie.equals("datetime")) {
			try {
				date = sdf.parse(value);
				date1 = DayFormat.afterNDay(date, -1);
				
				value = value+" 23:59:59";
				SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				date = sdf1.parse(value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Criterion e = Restrictions.between("datetime", date1, date);
			criteria.add(e);
		}else if (lie.equals("level")) {
			SimpleExpression f = Restrictions.eq("expressLevel", Integer.valueOf(value));
			criteria.add(f);
		}
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#insert(int, int, java.lang.String, java.lang.String, int, int, java.lang.String, java.lang.String)
	 */
	public void insert(String group_id,int expressUser_id,String fileName,String datetime,int expressLevel,int fileSize,String filePath,String remark) {
		String sql = "insert into dbo.t_fileDocument_file(group_id,expressUser_id,fileName,[datetime],expressLevel,fileSize,filePath,remark,state) values('"+group_id+"',"+expressUser_id+",'"+fileName+"','"+datetime+"',"+expressLevel+","+fileSize+",'"+filePath+"','"+remark+"',1)";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#updateState(java.lang.String)
	 */
	public void updateState(String filename) {
		String sql = "update dbo.t_fileDocument_file set state=0 where fileName in('"+filename+"')";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFileBo#updateFileName(int, java.lang.String, java.lang.String)
	 */
	public String updateFileName(String id,String fileName,String dirName) {
		String str = "";
		while (fileName.indexOf("  ")!=-1) {
			fileName = fileName.replace("  ", " ");
		}
		List list = documentFileDAO.findByProperty("fileName", fileName);
		if (list.size()>0) {
			FileDocumentFile file = (FileDocumentFile) list.iterator().next();
			str = "所修改文档名称与<"+file.getFileDocumentGroup().getGroupName()+">种类下的<"+fileName+">相冲突，请修改文档名称重新发布！";
		}else{
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		SimpleExpression a = Restrictions.eq("id", id);
		criteria.add(a);
		FileDocumentFile expressFile = (FileDocumentFile) qbcDao.getCriteria(criteria).iterator().next();
		
		String sql = "update dbo.t_fileDocument_file set fileName='"+fileName+"' where id='"+id+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
		
		dirName += "upload\\fileDocument\\";
		String strdos = "cmd /c rename \""+dirName+expressFile.getFileName()+"\" \""+fileName+"\"";
		Process process;
		try {
			process = Runtime
					.getRuntime().exec(strdos);
			process.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		str = "ok";
		}
		return str;
	}
}
